<?php

namespace app\api\controller;

use app\admin\model\user\Withdraw;
use app\common\controller\Api;
use app\common\model\MoneyLog;
use think\Config;
use think\Db;

/**
 *
 * 我的推广中心
 */
class Mc extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    public function friends (){

        $page = $this->request->get('page',1);
        $result = \app\common\model\User::where(['referee'=>$this->uid()])->field('id,avatar,nickname,mobile,createtime')->page($page)->select();
        $this->success('',$result);


    }

    public function moneyLog(){
        $page = $this->request->get('page',1);
        $result=MoneyLog::where(['uid'=>$this->uid()])->order('id','desc')->page($page)->select();
        $this->success('',$result);

    }

    public function earningsLog(){
        $page = $this->request->get('page',1);
        $result=Db::name('user_earnings_log')->where(['uid'=>$this->uid()])->order('id','desc')->page($page)->select();
        $this->success('',$result);

    }
    public function withdraw(){

        $money = $this->request->post('money',1);
        $type = $this->request->post('type',2);
        $user = $this->auth->getUser();
        $min = Config::get('site.withdraw_min') ?? 10;
        $max = Config::get('site.withdraw_max') ?? 1000;

        if($money < $min) $this->error('提现金额最低'.$min.'元');
        if($money > $max) $this->error('提现金额最大'.$max.'元');
        if($type==1) {
            if ($user->money < $money) $this->error('提现金额大于余额');
            $result = (new Withdraw())->run($user, $money,$type);
        }else{
            if ($user->earnings < $money) $this->error('提现金额大于余额');
            $result = (new Withdraw())->run($user, $money,$type);
        }
        $this->success('',$result);

    }

    public function withdrawlog(){
        //1046261869
        $page = $this->request->get('page',1);
        $result=Withdraw::where(['uid'=>$this->uid()])->order('id','desc')->page($page)->select();
        $this->success('',$result);

    }
}
